﻿Public Partial Class VehicleMaster
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "VESSEL MASTER "
            ViewState("gridVehicle") = clsGet.Get_VehicleMaster("")
            gridVehicle.DataSource = ViewState("gridVehicle")
            gridVehicle.DataBind()
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnUpdate.Visible = False
            End If
        End If

    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        Dim ClassSAP As New Class_SAP
        Dim SAP_Data As Object = ClassSAP.SAP_OPEN()
        Dim dsConfig As DataSet = Session("Config")
        Dim Im_Veh_From As String = ""
        Dim Im_Veh_To As String = ""
        Dim Date_From As String = ""
        Dim Date_To As String = ""
        If (radioTypeConnect.SelectedValue = "0") Then
            Im_Veh_From = txtFrom.Text
            Im_Veh_To = txtTo.Text
            Date_From = ""
            Date_To = ""
        Else
            Im_Veh_From = ""
            Im_Veh_To = ""
            Date_From = dsConfig.Tables(0).Rows(0).Item("Vehicle_Date")
            Date_To = Date.Now.AddDays(1).ToString("yyyyMMdd", New Globalization.CultureInfo("en-US"))
        End If
        Dim Ex_Subrc As Integer = 0

        Dim Veh_Table As New SAP_IPORT.ZBAPI_VEH_INPUTTable
        Dim Veh_Infor As New SAP_IPORT.ZBAPI_VEH_INFORTable
        Dim Veh_Unit As New SAP_IPORT.ZBAPI_VEH_UNITTable

        Try  'ส่งวันที่ update ล่าสุด จนถึงวันปัจจุบัน ใน Im_Cre_From , Im_Cre_To
            SAP_Data.Zbapi_Iport_Vehicle_Master(Date_From, Date_To, Im_Veh_From, Im_Veh_To, Ex_Subrc, Veh_Table, Veh_Infor, Veh_Unit)
            If (Veh_Unit.Count > 0) Or (Veh_Infor.Count > 0) Then
                Dim conn As New System.Data.SqlClient.SqlConnection(connString)
                Dim clsDB As New ClassDatabase()
                Dim clsFc As New ClassFunction()
                Dim condition As Boolean = True
                Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
                Try

                    Application.Lock()
                    conn.Open()
                    ' Dim ID_Customer As String = clsDB.GeneratorID(conn, "Customer")
                    ' Dim ID_BillTo As String = clsDB.GeneratorID(conn, "Customer_Billto")
                    tran = clsDB.BeginTran(conn)
                    For i As Integer = 0 To Veh_Infor.Count - 1
                        If (clsFc.UPDATEVehicle(Veh_Infor.Item(i).Vehicle, Veh_Infor.Item(i).Veh_Text, Veh_Infor.Item(i).Owner, Veh_Infor.Item(i).Reg_Cntry, Veh_Infor.Item(i).Veh_Type, Veh_Infor.Item(i).Veh_Length, Veh_Infor.Item(i).Veh_Width, Veh_Infor.Item(i).Veh_Height, Veh_Infor.Item(i).Draft, Veh_Infor.Item(i).Dim_Uom, Veh_Infor.Item(i).Veh_Unlwgt, Veh_Infor.Item(i).Veh_Unlwgt_C, Veh_Infor.Item(i).Veh_Maxwgt, Veh_Infor.Item(i).Veh_Maxwgt_C, Veh_Infor.Item(i).Wgt_Uom, Veh_Infor.Item(i).Veh_Maxvol, Veh_Infor.Item(i).Veh_Maxvol_C, Veh_Infor.Item(i).Vol_Uom, Veh_Infor.Item(i).Stcmnr, Veh_Infor.Item(i).Veh_Nrtus, conn, tran) = 0) Then
                            If (clsFc.InsertVehicle(Veh_Infor.Item(i).Vehicle, Veh_Infor.Item(i).Veh_Text, Veh_Infor.Item(i).Owner, Veh_Infor.Item(i).Reg_Cntry, Veh_Infor.Item(i).Veh_Type, Veh_Infor.Item(i).Veh_Length, Veh_Infor.Item(i).Veh_Width, Veh_Infor.Item(i).Veh_Height, Veh_Infor.Item(i).Draft, Veh_Infor.Item(i).Dim_Uom, Veh_Infor.Item(i).Veh_Unlwgt, Veh_Infor.Item(i).Veh_Unlwgt_C, Veh_Infor.Item(i).Veh_Maxwgt, Veh_Infor.Item(i).Veh_Maxwgt_C, Veh_Infor.Item(i).Wgt_Uom, Veh_Infor.Item(i).Veh_Maxvol, Veh_Infor.Item(i).Veh_Maxvol_C, Veh_Infor.Item(i).Vol_Uom, Veh_Infor.Item(i).Stcmnr, Veh_Infor.Item(i).Veh_Nrtus, conn, tran) < 1) Then
                                condition = False
                            End If
                        End If
                    Next
                    For i As Integer = 0 To Veh_Unit.Count - 1
                        If (clsFc.UPDATEVehicle_infor(Veh_Unit.Item(i).Vehicle, Veh_Unit.Item(i).Tu_Type, Veh_Unit.Item(i).Tu_Class, Veh_Unit.Item(i).Tu_Mode, Veh_Unit.Item(i).Tu_Length, Veh_Unit.Item(i).Tu_Width, Veh_Unit.Item(i).Tu_Height, Veh_Unit.Item(i).Dim_Uom, Veh_Unit.Item(i).Tu_Unlwgt, Veh_Unit.Item(i).Tu_Unlwgt_C, Veh_Unit.Item(i).Tu_Maxwgt, Veh_Unit.Item(i).Tu_Maxwgt_C, Veh_Unit.Item(i).Wgt_Uom, Veh_Unit.Item(i).Tu_Maxvol, Veh_Unit.Item(i).Tu_Maxvol_C, Veh_Unit.Item(i).Vol_Uom, conn, tran) = 0) Then
                            If (clsFc.InsertVehicle_infor(Veh_Unit.Item(i).Vehicle, Veh_Unit.Item(i).Tu_Type, Veh_Unit.Item(i).Tu_Class, Veh_Unit.Item(i).Tu_Mode, Veh_Unit.Item(i).Tu_Length, Veh_Unit.Item(i).Tu_Width, Veh_Unit.Item(i).Tu_Height, Veh_Unit.Item(i).Dim_Uom, Veh_Unit.Item(i).Tu_Unlwgt, Veh_Unit.Item(i).Tu_Unlwgt_C, Veh_Unit.Item(i).Tu_Maxwgt, Veh_Unit.Item(i).Tu_Maxwgt_C, Veh_Unit.Item(i).Wgt_Uom, Veh_Unit.Item(i).Tu_Maxvol, Veh_Unit.Item(i).Tu_Maxvol_C, Veh_Unit.Item(i).Vol_Uom, conn, tran) < 1) Then
                                condition = False
                            End If
                        End If

                    Next
                    If (Date_To <> "") Then
                        If (clsFc.UpdateVehicle_Date(Date.Now.ToString("yyyyMMdd", New Globalization.CultureInfo("en-US")), conn, tran) < 1) Then
                            condition = False
                        End If
                    End If

                    If (condition) Then
                        clsDB.CommitTran(tran)
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ');", True)
                    Else
                        clsDB.RollBackTran(tran)
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
                    End If
                Catch ex As Exception
                    clsDB.RollBackTran(tran)
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
                End Try
                conn.Close()
                Application.UnLock()
                Veh_Infor.Clear()
                Veh_Unit.Clear()
                conn.Dispose()


            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูล');", True)
            End If
        Catch ex As Exception
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดปัญหาในการเชื่อมต่อ SAP');", True)
        End Try
        ClassSAP.SAP_CLOSE(SAP_Data)
    End Sub

    Private Sub gridVehicle_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridVehicle.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridVehicle.PageIndex <> 0) Then
            Index = Index Mod (gridVehicle.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Session("VEHICLE") = gridVehicle.DataKeys(Index).Values("VEHICLE") & "$" & gridVehicle.DataKeys(Index).Values("VEH_TEXT") & "$" & gridVehicle.DataKeys(Index).Values("VEH_LENGTH").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("VEH_WIDTH").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("VEH_HEIGHT").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("DRAFT").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("VEH_MAXWGT").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("STCMNR").ToString().Trim().Replace("GRT", "").Replace("=", "").Replace("MT", "") & "$" & gridVehicle.DataKeys(Index).Values("REG_CNTRY").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("OWNER").ToString().Trim() & "$" & gridVehicle.DataKeys(Index).Values("VEH_MAXVOL_C").ToString().Trim()
            ModalPopupExtender1.Show()
        End If
    End Sub

    Private Sub gridVehicle_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridVehicle.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridVehicle.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridVehicle.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridVehicle.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridVehicle.PageCount.ToString()
            End If
        End If
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridVehicle.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridVehicle.PageIndex = 0
            Case "prev"
                If (gridVehicle.PageIndex <> 0) Then
                    gridVehicle.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridVehicle.PageIndex <> gridVehicle.PageCount) Then
                    gridVehicle.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridVehicle.PageIndex = gridVehicle.PageCount
        End Select
        gridVehicle.DataSource = ViewState("gridVehicle")
        gridVehicle.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridVehicle.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridVehicle.PageIndex = ddlPage.SelectedIndex
        gridVehicle.DataSource = ViewState("gridVehicle")
        gridVehicle.DataBind()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        ViewState("gridVehicle") = clsGet.Get_VehicleMaster(txtVehicle.Text.Trim())
        gridVehicle.DataSource = ViewState("gridVehicle")
        gridVehicle.DataBind()
    End Sub

    Private Sub gridVehicle_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridVehicle.RowEditing

    End Sub

    Private Sub radioTypeConnect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioTypeConnect.SelectedIndexChanged
        If (radioTypeConnect.SelectedValue = "0") Then
            rowVessel.Visible = True
        Else
            rowVessel.Visible = False
        End If
        txtFrom.Text = String.Empty
        txtTo.Text = String.Empty
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class